package com.magic.smartcity.view.more.adapters

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModel
import androidx.recyclerview.widget.RecyclerView
import com.magic.smartcity.R
import com.magic.smartcity.beans.BusOrderBean
import com.magic.smartcity.databinding.ItemBusOrderListBinding
import com.magic.smartcity.view.more.viewmodels.BusViewModel

/**
 * @description:
 * @create: Create by Magic on 2022/2/25.
 */
class BusOrderListAdapter(val list: MutableList<BusOrderBean>, val viewModel: BusViewModel) :
    RecyclerView.Adapter<BusOrderListAdapter.ItemHolder>() {

    class ItemHolder(val binding: ItemBusOrderListBinding) : RecyclerView.ViewHolder(binding.root)

    private val TAG: String = "BusOrderListAdapter"
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder {
        val inflate = DataBindingUtil.inflate<ItemBusOrderListBinding>(
            LayoutInflater.from(parent.context),
            R.layout.item_bus_order_list,
            parent,
            false
        )
        return ItemHolder(inflate)
    }

    override fun onBindViewHolder(holder: ItemHolder, position: Int) {
        holder.binding.busOrderDetail = list[position]
        if (list[position].status == 0) {
            holder.binding.payOrder.setOnClickListener{
                list[position].paymentType = "电子支付"
                viewModel.payOrder(list[position])
                list.removeAt(position)
                notifyDataSetChanged()
            }
        }
    }

    override fun getItemCount() = list.size
}